package it.unibo.refolding.pico;

import it.unibo.refolding.common.CommonThings;
import it.unibo.refolding.pico.stripchart.JpHelper;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.shorts.ShortList;

import java.util.List;
import java.util.Objects;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

public class Parsed {
  public static final Parsed NOT_PARSED = new Parsed(
    null, null, null, ImmutableList.<PlainPicoData>of());

  public final ShortList z, def;
  public final IntList jp;
  public final List<PlainPicoData> picoDataList;

  public Parsed(ShortList z, ShortList def, IntList jp, List<PlainPicoData> picoDataList) {
    Preconditions.checkArgument(z == def || z.size() == def.size());
    this.z = z;
    this.def = def;
    this.jp = jp;
    this.picoDataList = Objects.requireNonNull(picoDataList);
  }

  public Parsed withDeflectionSensitivity(double fixedDeflectionSensitivityMPerV) {
    List<PicoData> a = PicoData.withDeflectionSensitivity(
      picoDataList, fixedDeflectionSensitivityMPerV);
    @SuppressWarnings("unchecked")
    List<PlainPicoData> b = (List<PlainPicoData>) (List<?>) a;
    return new Parsed(z, def, jp, b);
  }

  public Parsed withSpringConstant(double fixedSpringConstantNPerM) {
    List<PicoData> a = PicoData.withSpringConstant(picoDataList, fixedSpringConstantNPerM);
    @SuppressWarnings("unchecked")
    List<PlainPicoData> b = (List<PlainPicoData>) (List<?>) a;
    return new Parsed(z, def, jp, b);
  }

  public int iterations() {
    return picoDataList.size();
  }

  public boolean isEmpty() {
    return picoDataList.isEmpty();
  }

  public PicoData onlyPlainPicoData() {
    return CommonThings.only(picoDataList);
  }

  public static Parsed from(List<PlainPicoData> picoDataList) {
    return new Parsed(
      PicoUtil.fullZ(picoDataList), PicoUtil.fullDeflection(picoDataList),
      JpHelper.junctionPoints(picoDataList), picoDataList);
  }

  public PlainPicoData last() {
    return CommonThings.last(picoDataList);
  }
}
